package servlet;

import constant.ExamConstant;
import dao.DeptDao;
import model.Dept;
import model.Doctor;
import model.Patient;
import model.RegRecord;
import service.RegRecordService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@WebServlet("/query")
public class RecordQuery extends HttpServlet {
    protected void process(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
try {
    // 设置请求编码格式
    request.setCharacterEncoding("utf-8");
    // 设置响应编码格式
    response.setContentType("text/html;charset=utf-8");
    
    String beginDate = request.getParameter("beginDate");
    String endDate = request.getParameter("endDate");
    
    // 保存查询参数到session，供导出功能使用
    request.getSession().setAttribute("beginDate", beginDate);
    request.getSession().setAttribute("endDate", endDate);
    
    Object type = request.getSession().getAttribute(ExamConstant.SESSION_LOGIN_TYPE);
    if (ExamConstant.LOGIN_TYPE_DOCTOR.equals(type)) {
        try {
            Doctor model = (Doctor) request.getSession().getAttribute(ExamConstant.SESSION_LOGIN_USER);
            List<RegRecord> records = RegRecordService.query(null, model.getDeptId(), beginDate, endDate);
            request.setAttribute("records", records);
            request.getRequestDispatcher(ExamConstant.HOME_PAGE_DOCTOR).forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", "查询失败：" + e.getMessage());
            request.getRequestDispatcher(ExamConstant.HOME_PAGE_DOCTOR).forward(request, response);
        }
    } else if (ExamConstant.LOGIN_TYPE_PATIENT.equals(type)) {
        try {
            Patient model = (Patient) request.getSession().getAttribute(ExamConstant.SESSION_LOGIN_USER);
            List<RegRecord> records = RegRecordService.query(model.getId(), null, beginDate, endDate);
            List<Dept> depts = DeptDao.select(null, null, null);
            request.setAttribute("depts", depts);
            request.setAttribute("records", records);
            request.getRequestDispatcher(ExamConstant.HOME_PAGE_PATIENT).forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", "查询失败：" + e.getMessage());
            request.getRequestDispatcher(ExamConstant.HOME_PAGE_PATIENT).forward(request, response);
        }
    } else {
        request.setAttribute("error", "请先进行登录");
        request.getRequestDispatcher(ExamConstant.HOME_PAGE_LOGIN).forward(request, response);
    }
} catch (Exception e) {
    e.printStackTrace();
    request.setAttribute("error", "系统错误：" + e.getMessage());
    request.getRequestDispatcher(ExamConstant.HOME_PAGE_LOGIN).forward(request, response);
}
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException {
    try {
        process(req, resp);
    } catch (Exception e) {
        e.printStackTrace();
        req.setAttribute("error", "系统错误：" + e.getMessage());
        req.getRequestDispatcher(ExamConstant.HOME_PAGE_LOGIN).forward(req, resp);
    }
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException {
    try {
        process(req, resp);
    } catch (Exception e) {
        e.printStackTrace();
        req.setAttribute("error", "系统错误：" + e.getMessage());
        req.getRequestDispatcher(ExamConstant.HOME_PAGE_LOGIN).forward(req, resp);
    }
}
}
